<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryGoodsList" :inline="true" label-width="68px">
        <el-form-item label="商品名称">
          <el-select
            v-model="queryGoodsList.materialName"
            placeholder="请选择物料信息"
            clearable
            :style="{ width: '100%' }"
            remote
            :remote-method="searchSelectNccMaterial"
            filterable
            :loading="loading"
          >
            <el-option
              v-for="item in materialNameOptions"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="批号">
          <el-input
            v-model="queryGoodsList.node"
            placeholder="请输入商品批号"
            clearable
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              {{ btnTXT }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="0">全部</el-dropdown-item>
              <el-dropdown-item command="1">等于</el-dropdown-item>
              <el-dropdown-item command="2">不等于</el-dropdown-item>
              <el-dropdown-item command="3">包含</el-dropdown-item>
              <el-dropdown-item command="4">不包含</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getGoodsQuery"
            >查询</el-button
          >
          <el-button type="danger" icon="el-icon-refresh" @click="clearAll"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        :data="fenyeList"
        border
        height="605px"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column
          prop="code"
          label="商品编码"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="vbatchcode"
          label="批号"
          sortable
          align="center"
        ></el-table-column>
        <el-table-column
          prop="vvendbatchcode"
          label="供应商批号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="商品名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sku_num"
          label="实际数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sto_unit_id"
          label="货位号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="materialspec"
          label="商品规格"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="pd_date"
          label="生产日期"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="exp_date"
          label="有效期"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="cwarehouse_name"
          label="仓库名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="use_num"
          label="使用数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="freeze_num"
          label="冻结数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="retest_date"
          label="复验期"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="enable_num"
          label="可用数量"
          align="center"
        ></el-table-column>
      </el-table>
      <el-pagination
        background
        layout=" total,prev, pager, next, sizes,jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getSkuQuery } from "@/api/report/index.js";
// 获取NCC物料信息-pk  ---头部下拉框
import { getSelectNccMaterialInfo } from "@/api/dropDown.js";
export default {
  data() {
    return {
      queryGoodsList: {
        materialName: "",
        node: "",
      },
      materialNameOptions: [], // 存储搜索的物料信息
      goodsList: [],
      btnTXT: "全部",
      batchNoMode: 0,
      loading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
    };
  },
  computed: {
    // 根据当前页码和每页条数计算当前页的数据
    fenyeList() {
      const startIndex = (this.pageNum - 1) * this.pageSize;
      return this.goodsList.slice(startIndex, startIndex + this.pageSize);
    },
  },
  created() {
    this.getGoods();
  },
  methods: {
    //分页
    handleSizeChange(newsize) {
      this.pageNum = 1;
      this.pageSize = newsize;
    },
    handleCurrentChange(newpage) {
      this.pageNum = newpage;
    },
    /**
     * 远程搜索函数
     * 获取NCC物料信息
     */
    // 远程搜索函数 获取NCC物料信息
    searchSelectNccMaterial(query) {
      if (query !== "") {
        this.loading = true; // 开始加载
        const params = {
          materialName: query, // 传入输入的关键字
        };
        // 调用接口进行物料信息查询
        getSelectNccMaterialInfo(params)
          .then((response) => {
            if (response.code === 200) {
              // 更新物料选项
              this.materialNameOptions = response.data.map((item) => ({
                code: item.value, // 物料主键
                name: item.label, // 物料名称
              }));
            } else {
              this.materialNameOptions = [];
            }
          })
          .catch((error) => {
            console.error("查询物料信息失败:", error);
            this.materialNameOptions = [];
          })
          .finally(() => {
            this.loading = false; // 停止加载
          });
      } else {
        this.materialNameOptions = [];
      }
    },
    //查询
    getGoodsQuery() {
      this.getGoods();
    },
    getGoods() {
      let param = {
        skuId: this.queryGoodsList.materialName,
        batchNoMode: this.batchNoMode,
        pkBatchNo: this.queryGoodsList.node,
      };
      getSkuQuery(param).then((res) => {
        this.goodsList = res.data;
        this.total = res.data.length;
      });
    },
    //下拉菜单
    handleCommand(command) {
      if (command == 0) {
        this.batchNoMode = command;
        this.btnTXT = "全部";
        this.getGoodsQuery();
      } else if (command == 1) {
        this.batchNoMode = command;
        this.btnTXT = "等于";
        this.getGoodsQuery();
      } else if (command == 2) {
        this.batchNoMode = command;
        this.btnTXT = "不等于";
        this.getGoodsQuery();
      } else if (command == 3) {
        this.batchNoMode = command;
        this.btnTXT = "包含";
        this.getGoodsQuery();
      } else {
        this.batchNoMode = command;
        this.btnTXT = "不包含";
        this.getGoodsQuery();
      }
    },
    clearAll() {
      this.queryGoodsList.materialName = "";
      this.queryGoodsList.node = "";
      this.btnTXT = "全部";
      this.batchNoMode = 0;
      this.getGoodsQuery();
    },
  },
};
</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;
}

.el-card {
  margin-bottom: 20px;
}

.el-pagination {
  float: right;
  margin: 20px;
}
</style>
